package me.zingon.service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.zingon.dao.OwnerDao;
import me.zingon.dao.PackageDao;
import me.zingon.model.Owner;
import me.zingon.model.Package;
import org.springframework.web.util.HtmlUtils;

@Service
public class PackageService {

	@Autowired
	PackageDao packageDao;
	
	@Autowired
	OwnerDao ownerDao;
	
	@Autowired
	EmailService emailService;
	
	
	public List<Package> getPackages(){
		return packageDao.getPackages();
	}
	
	public int addPackage(String ownerPhone,final String packageType,final String packageCode,int mId){
		final Package pg=new Package();
		final Owner owner= ownerDao.getOwnerByPhone(ownerPhone);
		if(owner==null)
			return 0;
		pg.setoId(owner.getId());
		pg.setmId(mId);
		pg.setType(HtmlUtils.htmlEscape(packageType));
		pg.setCode(HtmlUtils.htmlEscape(packageCode));
		pg.setFirstTime(new Timestamp(new Date().getTime()));
		pg.setDeletedFlag(0); 
		final int id=packageDao.insertPackage(pg); 
		advice(owner.getEmail(), "您的【"+packageType+"】包裹，到了,\n\r单号【"+packageCode+"】", pg.getId());
		return id;
	}
	
	public void advice(final String to,final String msg,final int pId){
		emailService.addTask(to, msg, pId,null);
	}

	public void filePackage(int id) {
		packageDao.filePackage(id,new Timestamp(new Date().getTime()));
	}
}
